package com.bstek.ureport.definition.searchform;


public class CombotreeComponent implements Component {
    private String label;
    private String bindParameter;
    private String type;
    private String dataType;
    protected LabelPosition labelPosition=LabelPosition.top;

    @Override
    public String toHtml(RenderContext context) {
        StringBuffer sb=new StringBuffer();
        if(this.labelPosition.equals(LabelPosition.top)){
            sb.append("<div>");
        }else{
            sb.append("<div class='form-horizontal'>");
        }
        sb.append("<div class='form-group' style='margin:0px 0px 10px 0px'>");
        String componentId = context.buildComponentId(this);
        if(this.labelPosition.equals(LabelPosition.top)){
            sb.append("<span style='font-size:13px'>"+this.label+"</span>");
            sb.append("<input id='"+componentId+"'>");
        }else{
            sb.append("<span class='col-md-3' style='text-align:right;padding-right:1px;font-size:13px'>"+this.label+"</span>");
            sb.append("<div class='col-md-9' style='padding-left:1px;'>");
            sb.append("<input id='"+componentId+"'>");
            sb.append("</div>");
        }
        sb.append("</div>");
        sb.append("</div>");

        sb.append("<script>");
        sb.append("setTimeout(function () {" +
                "                $('#"+componentId+"').combotree({" +
                "                    multiple: true," +
                "                    editable: false," +
                "                    cascadeCheck:false," +
                "                    panelHeight: 'auto'," +
                "                    checkbox: true," +
                "                    data: [{id: 1,text: '分类',children: [{id: 11,text: '项目一'},{id: 12,text: '项目二'}]}]" +
                "                });" +
                "},200);");
        sb.append("</script>");
        return sb.toString();
    }

    @Override
    public String initJs(RenderContext context) {
        String name=getBindParameter();
        StringBuilder sb=new StringBuilder();
        sb.append("formElements.push(");
        sb.append("function(){");
        sb.append("if(''==='"+name+"'){");
        sb.append("alert('树形选择未绑定查询参数名，不能进行查询操作!');");
        sb.append("throw '树形选择未绑定查询参数名，不能进行查询操作!'");
        sb.append("}");
        sb.append("return {");
        sb.append("\""+name+"\":");
        sb.append("$('#"+context.buildComponentId(this)+"').combotree('getValue')");
        sb.append("}");
        sb.append("}");
        sb.append(");");
        return sb.toString();
    }

    @Override
    public String getType() {
        return this.type;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getBindParameter() {
        return bindParameter;
    }

    public void setBindParameter(String bindParameter) {
        this.bindParameter = bindParameter;
    }

    public void setType(String type) {
        this.type = type;
    }

    public LabelPosition getLabelPosition() {
        return labelPosition;
    }

    public void setLabelPosition(LabelPosition labelPosition) {
        this.labelPosition = labelPosition;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }
}
